package msc.ai.mas.creditcard;




import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OnlineStoreGui extends JFrame
{
	private OnlineStoreAgent myStoreAgt;
	private JTextField txtItemName,txtItemPrice,txtItemQty;
	
	public OnlineStoreGui(OnlineStoreAgent myStore) throws HeadlessException
	{
			
		super(myStore.getLocalName());
		
		this.myStoreAgt = myStore;
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3 , 2));
		p.add(new JLabel("Item Name"));
		txtItemName = new JTextField(15);
		p.add(txtItemName);
		p.add(new JLabel("Item Price:"));
		txtItemPrice = new JTextField(15);
		p.add(txtItemPrice);
		p.add(new JLabel("Item Qty:"));
		txtItemQty = new JTextField(15);
		p.add(txtItemQty);
		getContentPane().add(p, BorderLayout.CENTER);
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(1 , 2));
		
		JButton btnPrintList = new JButton("Print Item List");
		btnPrintList .addActionListener( new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				try 
				{
					System.out.println("Item List of Seller : " + myStoreAgt.getLocalName() );
					
					HashMap<String, SalesItem> itemList = myStoreAgt.getSellItemList();
					int i = 1;
					
					Set<String> keySet = itemList.keySet();
					
					for (String key : keySet)
					{
						SalesItem sellItem  = itemList.get(key);
						System.out.println(i + ":" + sellItem.getItemQty() + " items of Item '" + sellItem.getItemName() 
								+ "' priced at " + sellItem.getItemPrice() + "" );
						i++;
					}
					
					
//					for (SellItem sellItem : itemList)
//					{
//						System.out.println(i + ":" + sellItem.getItemQty() + " items of Item '" + sellItem.getItemName() 
//								+ "' priced at " + sellItem.getItemPrice() + "" );
//						i++;
//					}
					
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(OnlineStoreGui.this, "Error Printing. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		} );
		
		p2.add(btnPrintList);
		
		JButton btnAddItem = new JButton("Add Item");
		btnAddItem.addActionListener( new ActionListener() 
		{
			public void actionPerformed(ActionEvent ev) 
			{
				try 
				{
					String itemName = txtItemName.getText().trim();
					double price = Double.parseDouble(txtItemPrice.getText().trim());
					int qty = Integer.parseInt(txtItemQty.getText().trim());
					SalesItem item = new SalesItem(itemName, price, qty);
					
					myStoreAgt.addItem(item);
					
					
					txtItemName.setText("");
					txtItemPrice.setText("");
					txtItemQty.setText("");
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(OnlineStoreGui.this, "Invalid values. "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		} );
//		p = new JPanel();
		p2.add(btnAddItem);
		
		
		getContentPane().add(p2, BorderLayout.SOUTH);
		
		// Make the agent terminate when the user closes 
		// the GUI using the button on the upper right corner	
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myStoreAgt.doDelete();
			}
		} );
		
		setResizable(false);
	}
	
	public void showGui() 
	{
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		OnlineStoreGui gui = new OnlineStoreGui(new OnlineStoreAgent());
		gui.showGui();
	}
	
	
	
}
